<?php
/**
 * Prado Portal.
 *
 * @author Steen Rabol <steen.rabol@gmail.com>
 * @link http://www.pradoportal.dk/
 * @copyright Copyright &copy; 2006 Steen Rabol
 * @license http://www.pradoportal.dk
 * @version $Id: DownloadPortlet.php 289 2008-11-14 05:34:40Z steen.rabol@gmail.com $
 *
 */

Prado::using('Application.Portlets.Portlet');
Prado::using('Application.Portlets.DownloadPortlet.Common.DownloadPortletGrpRecord');
Prado::using('Application.Portlets.DownloadPortlet.Common.DownloadPortletFileRecord');
Prado::using('Application.Common.Data.PortalRoleRecord');

class DownloadPortlet extends Portlet
{
	private	$_savePath;

	public function onInit($p)
	{
		$this->_savePath	= $this->Application->PortalBasePath	. '/storage/DownloadPortlet/';
		PortalUtil::CreateDirStructure($this->_savePath);
		parent::onInit($p);
	}

	public function onLoad($param)
	{
		parent::onLoad($param);
		if(!$this->Page->IsPostBack)
		{
			$this->bindData();

			if($this->findControl('dlDownloadGroups'))
			{
				$this->populateDownloadGroups();
			}

			// Set the MAX file size to the value in php.ini
			$this->FileUpload->MaxFileSize	= PortalUtil::return_bytes(ini_get('upload_max_filesize'));
			$this->ltFileSize->Text			= ini_get('upload_max_filesize');
		}
	}

	public function bindData()
	{
		$this->DLgroup->DataSource = DownloadPortletGrpRecord::finder()->findAllByactive("1");
		$this->DLgroup->dataBind();

		$this->UserRole->DataSource = $this->getUserRoles();
		$this->UserRole->dataBind();
	}

	public function getDownloadGroupsData()
	{
		$criteria = new TActiveRecordCriteria;
		$criteria->OrdersBy['name'] = 'asc';
		$criteria = new TActiveRecordCriteria;
		return DownloadPortletGrpRecord::finder()->findAll($criteria);
	}

	public function saveGroupButtonClicked($sender,$param)
	{
		if($this->Page->IsValid)
		{
			$grp				= new DownloadPortletGrpRecord();
			$grp->name			= $this->DownloadGroupName->Text;
			$grp->active		= TPropertyValue::ensureInteger($this->Active->Checked);
			$grp->role			= $this->UserRole->getSelectedValue();
			$grp->description	= $this->addGroupDescription->Text;
			$grp->save();
			
			$this->DownloadGroupName->Text = "";
			$this->dlDownloadGroups->VirtualItemCount = count($this->DownloadGroupsData);
			$this->populateDownloadGroups();

			if($this->dlDownloadGroups->SelectedItemIndex != -1)
			{
				$this->populateDownloadFiles($this->dlDownloadGroups->DataKeys[$this->dlDownloadGroups->getSelectedItemIndex()]);
			}
			else
			{
				$this->populateDownloadFiles(-1);
			}

			$this->bindData();
		}
	}

	public function validateGroupName($sender,$param)
	{
		$name	= $this->DownloadGroupName->Text;
		$id 	= $this->DataAccess->createCommand("select id from tbldownloadportletgrps where name='$name'")->queryScalar();

		if($id)
		{
			$param->IsValid = false;
		}
		else
		{
			$param->IsValid = true;
		}
	}

	public function onFileUpload($sender,$param)
	{
		if($sender->HasFile)
		{
			$sender->saveAs($this->_savePath . $sender->FileName);
			$dFile			= new DownloadPortletFileRecord();
			
			$dFile->grpid		= $this->DLgroup->getSelectedValue();
			$dFile->filename	= $sender->FileName;
			$dFile->comment		= $this->FileComment->Text;
			$dFile->description	= $this->FileDescription->Text;
			$dFile->active		= 1;
			$dFile->filesize	= $sender->FileSize;
			$dFile->filedate	= time();
			$dFile->save();

			$this->FileComment->Text		= "";
			$this->FileDescription->Text	= "";
			$this->Response->reload();
		}

	}

	public function getUserRoles()
	{
		return PortalRoleRecord::finder()->findAll();
	}

	public function RoleName($p_role)
	{
		return $this->DataAccess->createCommand("select name from tblroles where id=$p_role")->queryScalar();
	}

	// Download datalist
	public function editItemDlGroup($sender,$param)
	{
		$this->dlDownloadGroups->SelectedItemIndex	= -1;
		$this->dlDownloadGroups->EditItemIndex		= $param->Item->ItemIndex;
		$this->populateDownloadGroups();
		$this->populateDownloadFiles($this->dlDownloadGroups->DataKeys[$param->Item->ItemIndex]);
		$this->bindData();
	}

	public function cancelItemDlGroup($sender,$param)
	{
		$this->dlDownloadGroups->SelectedItemIndex	= $param->Item->ItemIndex;
		$this->dlDownloadGroups->EditItemIndex		= -1;
		$this->populateDownloadGroups();
		$this->populateDownloadFiles($this->dlDownloadGroups->DataKeys[$param->Item->ItemIndex]);
	}

	public function updateItemDlGroup($sender,$param)
	{
		$item				= $param->Item;
		$id					= $this->dlDownloadGroups->DataKeys[$item->ItemIndex];
		$grp				= DownloadPortletGrpRecord::finder()->findByi($id);
		$grp->name			= $item->Groupname->Text;
		$grp->description	= $item->GroupDescription->Text;
		$grp->role			= $item->grpUserRole->getSelectedValue();
		$grp->active		= TPropertyValue::ensureInteger($item->grpActive->Checked);
		$grp->save();

		$this->dlDownloadGroups->SelectedItemIndex	= $param->Item->ItemIndex;
		$this->dlDownloadGroups->EditItemIndex=-1;

		$this->populateDownloadGroups();
		$this->populateDownloadFiles($this->dlDownloadGroups->DataKeys[$param->Item->ItemIndex]);
		$this->bindData();
	}

	public function deleteItemDlGroup($sender,$param)
	{
		$id = $this->dlDownloadGroups->DataKeys[$param->Item->ItemIndex];
		DownloadPortletGrpRecord::finder()->deleteByid($id);
		
		if($param->Item->ItemIndex == $this->dlDownloadGroups->SelectedItemIndex)
			$this->dlDownloadGroups->SelectedItemIndex	= -1;

		$this->dlDownloadGroups->EditItemIndex		= -1;

		$this->populateDownloadGroups();
		if($this->dlDownloadGroups->SelectedItemIndex == -1)
		{
			$this->populateDownloadFiles(-1);
		}
		else
		{
			$this->populateDownloadFiles($this->dlDownloadGroups->DataKeys[$this->dlDownloadGroups->SelectedItemIndex]);
		}
		$this->bindData();
	}

	public function pageChangedDlGroup($sender,$param)
	{
		$this->dlDownloadGroups->CurrentPageIndex = $param->NewPageIndex;
		$this->populateDownloadGroups();
	}

	public function populateDownloadGroups()
	{
		$this->dlDownloadGroups->VirtualItemCount = count($this->DownloadGroupsData);

		$offset	= $this->dlDownloadGroups->CurrentPageIndex * $this->dlDownloadGroups->PageSize;
		$limit	= $this->dlDownloadGroups->PageSize;

		if($offset + $limit > $this->dlDownloadGroups->VirtualItemCount)
		{
			$limit = $this->dlDownloadGroups->VirtualItemCount - $offset;
		}

		if($offset >= $this->dlDownloadGroups->VirtualItemCount)
		{
			$offset = $this->dlDownloadGroups->VirtualItemCount - $this->dlDownloadGroups->PageSize;
			$limit	= $this->dlDownloadGroups->PageSize;
		}

		if($offset < 0)
			$offset = 0;

		$criteria = new TActiveRecordCriteria;
		$criteria->Limit			= $limit;
		$criteria->Offset			= $offset;
		$criteria->OrdersBy['name']	= 'asc';
		$criteria = new TActiveRecordCriteria;
		
		$this->dlDownloadGroups->DataSource	= DownloadPortletGrpRecord::finder()->findAll($criteria);
		$this->dlDownloadGroups->dataBind();
	}

	public function selectItemGroup($sender,$param)
	{
		// Populate files in this group
		$id = $this->dlDownloadGroups->DataKeys[$param->Item->ItemIndex];
		$this->populateDownloadFiles($id);
		$this->populateDownloadGroups();
	}

	public function populateDownloadFiles($grp)
	{
		if($grp == -1 || $grp === null)
		{
			$this->dlDownloadFiles->VirtualItemCount	= 0;
			$this->dlDownloadFiles->DataSource			= null;
			$this->dlDownloadFiles->dataBind();
			return;
		}
		
		$criteria = new TActiveRecordCriteria;
		$criteria->Condition = 'grpid = :grpid';
		$criteria->Parameters[':grpid'] = $grp;

		
		$this->dlDownloadFiles->VirtualItemCount		= DownloadPortletFileRecord::finder()->count($criteria);
		$offset	= $this->dlDownloadFiles->CurrentPageIndex * $this->dlDownloadFiles->PageSize;
		$limit	= $this->dlDownloadFiles->PageSize;

		if($offset + $limit > $this->dlDownloadFiles->VirtualItemCount)
		{
			$limit = $this->dlDownloadFiles->VirtualItemCount - $offset;
		}

		if($offset > $this->dlDownloadFiles->VirtualItemCount)
		{
			$offset = $this->dlDownloadFiles->VirtualItemCount - $this->dlDownloadFiles->PageSize;
			$limit	= $this->dlDownloadFiles->PageSize;
		}

		if($offset < 0)
		{
			$offset = 0;
		}

		$criteria = new TActiveRecordCriteria;
		$criteria->Condition			= 'grpid = :grpid';
		$criteria->Parameters[':grpid']	= $grp;
		$criteria->Limit			= $limit;
		$criteria->Offset			= $offset;
		
		$this->dlDownloadFiles->DataSource	= DownloadPortletFileRecord::finder()->findAll($criteria);
		$this->dlDownloadFiles->dataBind();
	}

	public function editItemDlFile($sender,$param)
	{
		$this->dlDownloadFiles->SelectedItemIndex=-1;
		$this->dlDownloadFiles->EditItemIndex=$param->Item->ItemIndex;
		$this->populateDownloadFiles($this->dlDownloadGroups->DataKeys[$this->dlDownloadGroups->getSelectedItemIndex()]);
	}

	public function cancelItemDlFile($sender,$param)
	{
		$this->dlDownloadFiles->SelectedItemIndex	= -1;
		$this->dlDownloadFiles->EditItemIndex		= -1;
		$this->populateDownloadFiles($this->dlDownloadGroups->DataKeys[$this->dlDownloadGroups->getSelectedItemIndex()]);
	}

	public function updateItemDlFile($sender,$param)
	{
		$item			= $param->Item;
		$id				= $this->dlDownloadFiles->DataKeys[$item->ItemIndex];
		$dFile	= DownloadPortletFileRecord::finder()->findByid($id);
		$dFile->description	= $item->edtDescription->Text;
		$dFile->comment		= $item->edtFileComment->Text;
		$dFile->active			= TPropertyValue::ensureInteger($item->edtFileActive->Checked);
		$dFile->filesize		= filesize($filename);
		$dFile->save();
		$this->dlDownloadFiles->EditItemIndex=-1;
		$this->populateDownloadFiles($this->dlDownloadGroups->DataKeys[$this->dlDownloadGroups->getSelectedItemIndex()]);
	}

	public function pageChangedDlFiles($sender,$param)
	{
		$this->dlDownloadFiles->CurrentPageIndex = $param->NewPageIndex;
		$this->populateDownloadFiles($this->dlDownloadGroups->DataKeys[$this->dlDownloadGroups->getSelectedItemIndex()]);
	}

	public function deleteItemDlFile($sender,$param)
	{
		$id = $this->dlDownloadFiles->DataKeys[$param->Item->ItemIndex];
		$dFile	= DownloadPortletFileRecord::finder()->deleteByid($id);
		$this->dlDownloadFiles->SelectedItemIndex=-1;
		$this->dlDownloadFiles->EditItemIndex=-1;
		$this->populateDownloadFiles($this->dlDownloadGroups->DataKeys[$this->dlDownloadGroups->getSelectedItemIndex()]);
		$this->bindData();
	}
}
?>